package com.tianpengtech.common.generator.api;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.kit.JsonKit;
import com.tianpengtech.common.generator.api.field.ApiField;
import com.tianpengtech.common.util.JsonUtil;

public class APIConfig {
	
	public static final String POST="POST";
	public static final String GET="GET";
	private String method="GET";
	private String url;
	private List<ApiField> fields = new ArrayList<ApiField>();
	private String result;
	private String descript;
	
	
	
	public String getDescript() {
		return descript;
	}
	public APIConfig setDescript(String descript) {
		this.descript = descript;
		return this;
	}
	public String getMethod() {
		return method;
	}
	public APIConfig setMethod(String method) {
		this.method = method;
		return this;
	}
	public String getUrl() {
		return url;
	}
	public APIConfig setUrl(String url) {
		this.url = url;
		return this;
	}
	public List<ApiField> getFields() {
		return fields;
	}
	public APIConfig setFields(List<ApiField> fields) {
		this.fields = fields;
		return this;
	}
	public String getResult() {
		return result;
	}
	public APIConfig setResult(String result) {
		this.result = result;
		return this;
	}
	
	
	public APIConfig addField(ApiField field){
		fields.add(field);
		return this;
	}
	
	public APIConfig renderSuccess(String msg){
		this.result = JsonKit.toJson(JsonUtil.printSuccess(msg));
		return this;
	}
	
	public APIConfig renderError(String msg){
		this.result = JsonKit.toJson(JsonUtil.printError(msg));
		return this;
	}
	
	public APIConfig renderData(Object obj){
		this.result = JsonKit.toJson(JsonUtil.printObject(obj));
		return this;
	}
	
	public String domain(String url){
		
		return "http://www.domain.com"+url;
	}
	
	public void excute(){
		System.out.println("### "+this.getDescript());

		System.out.println("### 请求地址 ");
		System.out.println("- "+this.getMethod()+" "+ domain(this.getUrl()));
		System.out.println("### 请求参数 ");
		System.out.println("```");
		System.out.println("{");
		for (ApiField apiField : fields) {
			if(apiField.isRequired()){
				System.out.println(apiField.getField()+":"+apiField.getValue()+"//"+apiField.getComment()+"(必填)");
			}else{
				System.out.println(apiField.getField()+":"+apiField.getValue()+"//"+apiField.getComment());
			}
		}
		System.out.println("}");
		System.out.println("```");
		
		System.out.println("### 请求结果");
		System.out.println("```");
		System.out.println(this.getResult());
		System.out.println("```");
	}
}
